package com.xja.git.controller;

import com.xja.git.entity.Book;
import com.xja.git.service.BookService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class BookController {
    @Autowired
    private BookService bookService;
    private Logger logger= LoggerFactory.getLogger(BookController.class);
    /**
     * 根据编号查询检查
     * @param session
     * @return
     */
    @GetMapping("select")
    public String select(HttpSession session){
        logger.info("根据id查询");
        Book book=bookService.queryById(1);
        if (book!=null){
            System.out.println(book);
        }else{
            System.out.println("查询失败");
        }
        return "查询成功";
    }
    /**
     * 添加
     * @return
     */
    @GetMapping("add")
    public String add() throws ParseException {
        logger.info("添加");
        int n = bookService.add(new Book("三国演义",26.0,"吴承恩"));
        if (n>0){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
        return "添加成功";
        }
        /**
         * 查全部
         * @param session
         * @return
         */
        @GetMapping("list")
        public String list(HttpSession session){
            List<Book> bookList = bookService.queryAll();
            System.out.println(bookList);
            return "成功";
        }
    @GetMapping("update")
        public String update() {
            logger.info("修改");
            int n=bookService.modify(new Book(2,"萌萌萌",23.0,"mmm"));
            if (n>0){
                System.out.println("修改成功");
                return "修改成功";
            }else{
                System.out.println("修改失败");
                return "修改失败";
            }
        }
    @GetMapping("delete")
        public String delete() {
            logger.info("删除");
            int n = bookService.remove(1);
            if (n>0){
                System.out.println("删除成功");
                return "删除成功";
            }else{
                System.out.println("删除失败");
                return "删除失败";
            }
        }
    @GetMapping("wangwei")
    public String wangwei() {
       return "wangwei";
    }
    @GetMapping("master")
    public String master() {
       return "master";
    }
    @GetMapping("libai")
    public String libai() {
       return "libai";
    }
}

